/*
 Copyright (c) 2013 MeherTJ G. All rights reserved.
 License: LGPL for personnal study or free software.
 */

#pragma once
#include "zen_al_platform.h"
#include "zen_log.h"

namespace Zen
{
	class ALPlayer
	{
	public:
		ALPlayer()
		{
			ALCcontext* context = nullptr;
			ALCdevice* device = alcOpenDevice(NULL); // open default device
			if (device == nullptr) __zen_throw("failed to open sound device");
			context = alcCreateContext(device, nullptr); // Generate context
			if (context == nullptr)
			{
				ALenum eno = alcGetError(device);
				alcCloseDevice(device);
				Zen::LogE("alcCreateContext error %d(0x%x)", (int)eno, (int)eno);
				__zen_throw("failed to Generate audio context");
			}
			if (alcMakeContextCurrent(context) != AL_TRUE)
			{
				ALenum eno = alcGetError(device);
				Zen::LogE("alcMakeContextCurrent error %d(0x%x)", (int)eno, (int)eno);
				alcDestroyContext(context);
				alcCloseDevice(device);
				__zen_throw("failed to set audio context");
			}
		}
		~ALPlayer()
		{
			ALCdevice* device = GetCurrentDevice();
			alcDestroyContext(GetCurrentContext());
			alcCloseDevice(device);
		}

		inline ALCdevice* GetCurrentDevice()
		{
			return alcGetContextsDevice(alcGetCurrentContext());
		}

		inline ALCcontext* GetCurrentContext()
		{
			return alcGetCurrentContext();
		}

		inline void playSound(ALuint sourceID)
		{
			alSourcePlay(sourceID);
		}

		inline void pauseSound(ALuint sourceID)
		{
			alSourcePause(sourceID);
		}

		inline void stopSound(ALuint sourceID)
		{
			alSourceStop(sourceID);
		}

		inline void playSounds(uint32_t count, ALuint const* sources)
		{
			alSourcePlayv((ALsizei)count, sources);
		}

		inline void pauseSounds(uint32_t count, ALuint const* sources)
		{
			alSourcePausev((ALsizei)count, sources);
		}

		inline void stopSounds(uint32_t count, ALuint const* sources)
		{
			alSourceStopv((ALsizei)count, sources);
		}
	};
} // namespace Zen
